//
//  XJPayPasswordView.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/5/24.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  支付密码view

#import "XJPayPasswordView.h"
#import "XJCodeView.h" // 验证码button

@interface XJPayPasswordView()<XJCodeDelegate>
@property(nonatomic, strong) XJCodeView *xjCodeView;
@property(nonatomic, strong) UIButton *nextButton;
@end
@implementation XJPayPasswordView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


#pragma mark - 一 初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView{
    // 手机号
    [self addSubview:self.ppPhoneTextField];
    
    // 验证码
    [self addSubview:self.ppCodeTextField];
    self.ppCodeTextField.rightView = self.xjCodeView;
    self.ppCodeTextField.rightViewMode = UITextFieldViewModeAlways;
    self.xjCodeView.delegate = self;
    
    // 下一步按钮
    [self addSubview:self.nextButton];
    
}

//ZYTextField * fpPhoneTextField = [[ZYTextField alloc]initWithFrame:CGRectMake(ADAPTWidth(23), ADAPTHeight(30), KWidth - ADAPTWidth(23)*2, ADAPTHeight(50)) placeholder:@"请输入手机号" keyboardType:UIKeyboardTypePhonePad leftViewImageName:@"login_phone" textEntry:NO];
//self.fpPhoneTextField = fpPhoneTextField;
//[self.fpPhoneTextField addTarget:self action:@selector(regist_phoneTextDidChange:) forControlEvents:(UIControlEventEditingChanged)];

#pragma mark - 二 懒加载
- (ZYTextField *)ppPhoneTextField {
    if (!_ppPhoneTextField) {
        _ppPhoneTextField = [[ZYTextField alloc] initWithFrame:CGRectMake(ADAPTWidth(23), ADAPTHeight(30), KWidth - ADAPTWidth(23)*2, ADAPTHeight(50)) placeholder:@"请输入手机号" keyboardType:(UIKeyboardTypePhonePad) leftViewImageName:@"login_phone" textEntry:NO];
        
        [_ppPhoneTextField addTarget:self action:@selector(regist_phoneTextDidChange:) forControlEvents:(UIControlEventEditingChanged)];
    }
    return _ppPhoneTextField;
}

- (ZYTextField *)ppCodeTextField {
    if (!_ppCodeTextField) {
        _ppCodeTextField = [[ZYTextField alloc] initWithFrame:CGRectMake(ADAPTWidth(23), ADAPTHeight(80), KWidth - ADAPTWidth(23)*2, ADAPTHeight(50)) placeholder:@"请输入验证码" keyboardType:(UIKeyboardTypePhonePad) leftViewImageName:@"login_verify" textEntry:NO];
    }
    return _ppCodeTextField;
}



- (XJCodeView *)xjCodeView {
    if (!_xjCodeView) {
        XJCodeView * xjCodeView = [[XJCodeView alloc]initWithTime:60 frame:CGRectMake(0, 0, 80, 30)];
        xjCodeView.codeBtn.userInteractionEnabled = NO;
        xjCodeView.codeBtn.backgroundColor = MAINS_COLOR;
        
        self.xjCodeView = xjCodeView;
    }
    return _xjCodeView;
}

- (UIButton *)nextButton {
    if(!_nextButton){
        _nextButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_nextButton setTitle:@"下一步" forState:(UIControlStateNormal)];
        [_nextButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        _nextButton.backgroundColor = XJCOLOR_RGB(255, 216, 67);
        _nextButton.layer.masksToBounds = YES;
        _nextButton.layer.cornerRadius = 10;
        _nextButton.frame = CGRectMake(ADAPTWidth(23), ADAPTHeight(80+80), KWidth - ADAPTWidth(23)*2, ADAPTHeight(50));
    
        [_nextButton addTarget:self action:@selector(nextButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _nextButton;
}


#pragma mark - 三 回调事件
// 监听手机号变化
- (void)regist_phoneTextDidChange:(ZYTextField *)phoneTF {

//    XJLog(@"手机号%@",phoneTF.text);
    // 手机号合法获取验证码 按钮可用
    if ([phoneTF.text isPhoneNumber]) {
        self.xjCodeView.codeBtn.backgroundColor = MAIN_COLOR;
        self.xjCodeView.codeBtn.userInteractionEnabled = YES;
    }else {
        self.xjCodeView.codeBtn.backgroundColor = MAINS_COLOR;
        self.xjCodeView.codeBtn.userInteractionEnabled = NO;
    }
}

// 验证验证码
- (void)getCheckCode:(NSString *)checkCode {
    XJLog(@"验证码是:%@",checkCode);
    // 这里写网络请求
    
    NSString *phone = [XJUserModel xj_phone];
    if([NSString isEmptyString:phone]) {
        XJLog(@"手机号为空,操作无效");
        return;
    }
    
    [XJHttpTool requestSendCodeWithPhone:phone type:@"editpwdpay" successH:^(id responseObjc) {
        
        NSString *code = [NSString stringWithFormat:@"%@",responseObjc[@"code"]];
        if ([code isEqualToString:@"0"]) {
            [JRToast showWithText:@"验证码已发送"];
        }else {
            [JRToast showWithText:responseObjc[@"message"]];
        }
    } failureH:^(NSError *error) {
        
        [JRToast showWithText:[error localizedDescription]];
    }];
    
}

// 下一步按钮事件
- (void)nextButtonAction {
    
    // 写验证验证码的网络请求，如果ok 下一步
    if (self.nextButtonBlock) {
        self.nextButtonBlock();
    }
}



#pragma mark - 接口
- (void)enableSendCodeButton {
    self.xjCodeView.codeBtn.backgroundColor = MAIN_COLOR;
    self.xjCodeView.codeBtn.userInteractionEnabled = YES;
}
@end
